/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.rocketmq.broker.transaction;

import org.apache.rocketmq.broker.BrokerController;
import org.apache.rocketmq.common.ServiceThread;
import org.apache.rocketmq.common.constant.LoggerName;
import org.apache.rocketmq.logging.InternalLogger;
import org.apache.rocketmq.logging.InternalLoggerFactory;

/**
 * 定时检查本地事务执行结果
 * 定时从事务消息queue中读出所有待反查的事务消息。
 * 事务消息检查器我们要从事务消息检查服务说起，它是一个任务，
 * 也就是实现runnable接口，里面有个线程，启动的时候，会执行它的run方法
 * @author
 */
public class TransactionalMessageCheckService extends ServiceThread {
    private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.TRANSACTION_LOGGER_NAME);

    private BrokerController brokerController;

    public TransactionalMessageCheckService(BrokerController brokerController) {
        this.brokerController = brokerController;
    }

    @Override
    public String getServiceName() {
        return TransactionalMessageCheckService.class.getSimpleName();
    }

    /**
     * 定时检查本地事务执行结果，每60s执行一次
     * 通过参数配置 transactionCheckInterval = 60 * 1000
     */
    @Override
    public void run() {
        log.info("Start transaction check service thread!");
        // 检查间隔 transactionCheckInterval = 60 * 1000
        long checkInterval = brokerController.getBrokerConfig().getTransactionCheckInterval();
        while (!this.isStopped()) {
            this.waitForRunning(checkInterval);
        }
        log.info("End transaction check service thread!");
    }

    /**
     * 这个onWaitEnd 方法里面先是获取了一个timeout值，其实就是消息存入half topic后没有本地事务执行结果会检查，这里默认是6s。
     * 接着就是最大检查次数，默认是15次，最后就是调用事务消息服务组件check方法了。
     * 这个方法其实就是对比half topic 与那个op half topic里面的消息，
     * 然后那种找出那种6s后还没有本地消息执行结果的，也就是没有在op half topic 删除的消息。找到之后，
     * 就会重新塞入到half topic中一次，接着就是通知消息生产者，问问消息生产者本地消息执行ok了么
     */
    @Override
    protected void onWaitEnd() {
        // 开始检查时间 这个就是事务在6s之后 还在half就检查 transactionTimeOut = 6 * 1000
        long timeout = brokerController.getBrokerConfig().getTransactionTimeOut();
        // 检查最大次数，超过了这个值就会被干掉了，最大检查次数是15次
        int checkMax = brokerController.getBrokerConfig().getTransactionCheckMax();
        long begin = System.currentTimeMillis();
        log.info("Begin to check prepare message, begin time:{}", begin);
        this.brokerController.getTransactionalMessageService().check(timeout, checkMax,
                this.brokerController.getTransactionalMessageCheckListener());
        log.info("End to check prepare message, consumed time:{}", System.currentTimeMillis() - begin);
    }

}
